<?php

class DBConnection 
{
	private static $openConnections = 0;
	private $query;
	
	private static $instance = null;
	
	private function __construct()
	{
		$this->open();
	}
	
	private function open()
	{
		$server = 'localhost';
		$username = 'root';
		$password = 'root';
		$dbname = 'lotrogear';
		
		$this->conn = mysql_connect( $server, $username, $password);
		mysql_select_db( $dbname);

	}
	
	private function close()
	{
		$this->openConnections--;
		if ($openConnections <= 0 )
		{
			mysql_close( $this->conn);
		} 
	}
	
	public function setupQuery( $querystring)
	{
		$this->query = $querystring;
	}
	
	public function executeQuery( )
	{
		$result = mysql_query( $this->query);
		
		if ($result === false)
		{
			md_sql_error( mysql_error());
			throw new Exception(mysql_error());
		}
		
		$out = array();
		while ( $row = mysql_fetch_assoc( $result))
		{
			$out[] = $row;
		}

		return $out;
		
	}
	
	public static function getConnection()
	{
		self::$openConnections++;
		
		if (self::$instance == null)
		{
			self::$instance = new DBConnection();
		}
		
		return self::$instance;
	}
	
}	//end of class 

